package com.vitabuybuy.tv.ui.cart.adapter

import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemStoreBinding
import com.vitabuybuy.tv.dto.SettlementDto
import com.library.common.base.BindRecycAdapter
import com.vitabuybuy.tv.ui.cart.ConfirmOrderActivity
import kotlinx.android.synthetic.main.activity_confirm_order.*
import java.math.BigDecimal

class StoreAdapter(val from: Int, val liveId: Int?) :
    BindRecycAdapter<ItemStoreBinding, SettlementDto.StoreListBean>() {
    private lateinit var goodItemAdapter: GoodItemAdapter
    override fun onBindViewHolder(
        mBinding: ItemStoreBinding,
        itemData: SettlementDto.StoreListBean,
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        mBinding.chooseCouponLayout.visibility = when (from) {
            ConfirmOrderActivity.GROUP_BUY_CREATE, ConfirmOrderActivity.GROUP_BUY_JOIN, ConfirmOrderActivity.GROUP_BUY_BY_LIMIT_JOIN -> View.GONE
            else -> if (liveId == 0 || liveId == null) View.VISIBLE else View.GONE
        }
        mBinding.itemData = itemData
        itemData.discountAmount.run {
            if (this != 0.0) {
                mBinding.tvCouponReduction.text = "- ".plus(
                    String.format(
                        mContext.getString(R.string.price_with_value),
                        this
                    )
                )
                mBinding.tvCoupons.text = String.format(
                    mContext.getString(R.string.yi_youhui),
                    this
                )
            } else {
                mBinding.tvCoupons.text = mContext.getString(R.string.please_choose)
                mBinding.tvCouponReduction.text = "- 0.00"
            }
        }
        goodItemAdapter = GoodItemAdapter()
        mBinding.rcvOrder.apply {
            adapter = goodItemAdapter
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
            setHasFixedSize(true)
            isNestedScrollingEnabled = false
        }
        goodItemAdapter.setNewData(itemData.itemList)
        mBinding.chooseCouponLayout.setOnClickListener {
            chooseStoreCouponListener.invoke(position)
        }
        mBinding.etRemark.addTextChangedListener {
            remarkListener.invoke(itemData.id, it.toString())
        }
    }

    override fun normalViewId(viewType: Int): Int {
        return R.layout.item_store
    }


    lateinit var chooseStoreCouponListener: (position: Int) -> Unit
    lateinit var remarkListener: (stordId: Int, remark: String) -> Unit
}
